package com.itheima.behavior.controller.v1;

import com.itheima.behavior.service.RecommendService;
import com.itheima.model.behavior.dtos.RecommendQueryDto;
import com.itheima.model.common.dtos.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 推荐控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/v1/recommend")
@Api(value = "推荐管理API", tags = "推荐管理")
public class RecommendController {

    @Autowired
    private RecommendService recommendService;

    /**
     * 获取个性化推荐
     */
    @GetMapping("/personalized")
    @ApiOperation("获取个性化推荐")
    public ResponseResult getPersonalizedRecommend(
            @ApiParam("推荐数量") @RequestParam(required = false, defaultValue = "10") Integer limit) {
        return recommendService.getPersonalizedRecommend(limit);
    }

    /**
     * 获取协同过滤推荐
     */
    @GetMapping("/collaborative")
    @ApiOperation("获取协同过滤推荐")
    public ResponseResult getCollaborativeRecommend(
            @ApiParam("推荐数量") @RequestParam(required = false, defaultValue = "10") Integer limit) {
        return recommendService.getCollaborativeRecommend(limit);
    }

    /**
     * 获取热门推荐
     */
    @GetMapping("/popular")
    @ApiOperation("获取热门推荐")
    public ResponseResult getPopularRecommend(
            @ApiParam("推荐数量") @RequestParam(required = false, defaultValue = "10") Integer limit) {
        return recommendService.getPopularRecommend(limit);
    }

    /**
     * 获取分类推荐
     */
    @GetMapping("/category")
    @ApiOperation("获取分类推荐")
    public ResponseResult getCategoryRecommend(
            @ApiParam("分类ID") @RequestParam Integer categoryId,
            @ApiParam("推荐数量") @RequestParam(required = false, defaultValue = "10") Integer limit) {
        return recommendService.getCategoryRecommend(categoryId, limit);
    }

    /**
     * 获取相似图书推荐
     */
    @GetMapping("/similar")
    @ApiOperation("获取相似图书推荐")
    public ResponseResult getSimilarBooksRecommend(
            @ApiParam("图书ID") @RequestParam Integer bookId,
            @ApiParam("推荐数量") @RequestParam(required = false, defaultValue = "6") Integer limit) {
        return recommendService.getSimilarBooksRecommend(bookId, limit);
    }

    /**
     * 混合推荐（猜你喜欢）
     */
    @GetMapping("/hybrid")
    @ApiOperation("混合推荐（猜你喜欢）")
    public ResponseResult getHybridRecommend(
            @ApiParam("推荐数量") @RequestParam(required = false, defaultValue = "10") Integer limit) {
        return recommendService.getHybridRecommend(limit);
    }

    /**
     * 获取猜你喜欢（别名接口，兼容小程序）
     */
    @GetMapping("/guess")
    @ApiOperation("猜你喜欢")
    public ResponseResult getGuessYouLike(
            @ApiParam("推荐数量") @RequestParam(required = false, defaultValue = "10") Integer limit) {
        return recommendService.getHybridRecommend(limit);
    }

    /**
     * 获取新书推荐
     */
    @GetMapping("/new")
    @ApiOperation("获取新书推荐")
    public ResponseResult getNewBooksRecommend(
            @ApiParam("推荐数量") @RequestParam(required = false, defaultValue = "10") Integer limit) {
        return recommendService.getNewBooksRecommend(limit);
    }

    /**
     * 发现好书（别名接口，兼容小程序）
     */
    @GetMapping("/discovery")
    @ApiOperation("发现好书")
    public ResponseResult getDiscoveryRecommend(
            @ApiParam("推荐数量") @RequestParam(required = false, defaultValue = "10") Integer limit) {
        return recommendService.getNewBooksRecommend(limit);
    }

    /**
     * 通用推荐接口（兼容小程序）
     * 支持 type 参数：recommend, guess, popular, hot, premium, discovery
     */
    @GetMapping
    @ApiOperation("通用推荐接口")
    public ResponseResult getRecommend(
            @ApiParam("推荐类型") @RequestParam(required = false) String type,
            @ApiParam("推荐数量") @RequestParam(required = false, defaultValue = "10") Integer limit,
            @ApiParam("分类ID") @RequestParam(required = false) Integer categoryId,
            @ApiParam("图书ID") @RequestParam(required = false) Integer bookId) {
        
        RecommendQueryDto dto = new RecommendQueryDto();
        dto.setType(type);
        dto.setLimit(limit);
        dto.setCategoryId(categoryId);
        dto.setBookId(bookId);
        
        return recommendService.getRecommend(dto);
    }

    /**
     * POST方式的通用推荐接口
     */
    @PostMapping
    @ApiOperation("通用推荐接口（POST）")
    public ResponseResult getRecommendByPost(@RequestBody RecommendQueryDto dto) {
        return recommendService.getRecommend(dto);
    }
}

